package com.ruitu.mall_98_2.mine;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.beanu.arad.base.my_base.MyBaseActivity;
import com.beanu.arad.base.my_base.base_list.ListBaseAdapter;
import com.beanu.arad.base.my_base.base_list.SuperViewHolder;
import com.beanu.arad.utils.ToastUtils;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.ruitu.mall_98_2.R;
import com.ruitu.mall_98_2.dao.APIRetrofit;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.SubscriberImpl;
import com.ruitu.router_module.bean.mine.ShopIntegral;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class IntegralDetailActivity extends MyBaseActivity {
    private RecyclerView rcv_list;

    private IntegralAdapter adapter;
    private List<ShopIntegral> integralList = new ArrayList<>();

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_integral_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rcv_list = findViewById(R.id.rcv_list);

        adapter = new IntegralAdapter(this);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);

        reqShopIntegral();
        showProgress();

        setHeadTitle("积分详情");
    }

    // 获取积分列表数据
    private void reqShopIntegral() {
        APIRetrofit.getDefault().reqIntegralDetailList(AppHolder.instance().getUser().getAccountId())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new SubscriberImpl<BaseResult<List<ShopIntegral>>>() {
                    @Override
                    public void onError(Throwable e) {
                        hideProgress();
                        ToastUtils.showShort("获取积分详情失败");
                    }

                    @Override
                    public void onNext(BaseResult<List<ShopIntegral>> listResult) {
                        hideProgress();
                        if (listResult.isSuccess()) {
                            integralList = listResult.getData();
                            adapter.setDataList(integralList);
                        } else {
                            ToastUtils.showShort(listResult.getMsg());
                        }
                    }
                });
    }

    private class IntegralAdapter extends ListBaseAdapter<ShopIntegral> {

        public IntegralAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_shop_integral;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_shop_name = holder.getView(R.id.tv_shop_name);
            TextView tv_time = holder.getView(R.id.tv_time);
            TextView tv_integral = holder.getView(R.id.tv_integral);

            ShopIntegral integral = mDataList.get(position);
            tv_shop_name.setText(integral.getCompanyName());
            try {
                tv_time.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(integral.getUpdateTime()));
            } catch (Exception e) {
                e.printStackTrace();
                tv_time.setText("积分获取时间异常");
            }
            tv_integral.setText(integral.getNegativeIntegral() + "");
        }
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return null;
    }
}
